﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MedicalLog.Domain.Entities;

namespace MedicalLog.Domain.Repositories
{
    public class VitalSignRepository
    {
        MedicalLogEntities _dbMedicalLog;

        public VitalSignRepository()
        {
            _dbMedicalLog = new MedicalLogEntities();
        }

        /// <summary>
        /// This methods adds a new vital sign to the database
        /// </summary>
        /// <param name="newVitalSign"></param>
        public void AddVitalSign(VitalSing newVitalSign)
        {
            _dbMedicalLog.AddToVitalSings(newVitalSign);
            _dbMedicalLog.SaveChanges();
        }

        /// <summary>
        /// This method returns the detail of the vital sign with the provided id
        /// </summary>
        /// <param name="pId"></param>
        /// <returns></returns>
        public VitalSing Detail(int pId)
        {
            return _dbMedicalLog.VitalSings.First(x => x.Id == pId);
        }

        /// <summary>
        /// This method updates the selected vitalsign
        /// </summary>
        /// <param name="oldVitalSign"></param>
        public void UpdateVitalSign(VitalSing oldVitalSign)
        {
            _dbMedicalLog.VitalSings.FirstOrDefault(x => x.Id == oldVitalSign.Id);
            _dbMedicalLog.VitalSings.ApplyCurrentValues(oldVitalSign);
            _dbMedicalLog.SaveChanges();
        }

        /// <summary>
        /// This method deletes an existing Vital Sign
        /// </summary>
        /// <param name="pId"></param>
        public void DeleteVitalSign(int pId)
        {
            var vitalSign = _dbMedicalLog.VitalSings.First(x => x.Id == pId);
            if (vitalSign != null)
            {
                _dbMedicalLog.VitalSings.DeleteObject(vitalSign);
                _dbMedicalLog.SaveChanges();
            }
        }

        /// <summary>
        /// This method deletes and existing Vital Sign
        /// </summary>
        /// <param name="vitalSign"></param>
        public void DeleteVitalSign(VitalSing vitalSign)
        {
            _dbMedicalLog.VitalSings.DeleteObject(vitalSign);
            _dbMedicalLog.SaveChanges();
        }
    }
}
